package com.dlc.shop.search.common.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.bean.event.SkuDeleteEvent;
import com.dlc.shop.bean.model.SkuCombo;
import com.dlc.shop.bean.model.StockBillLogItem;
import com.dlc.shop.bean.vo.SkuStockVO;
import com.dlc.shop.bean.vo.StockPointSkuVO;
import com.dlc.shop.common.constants.LuaOperateEnum;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.manager.impl.StockManager;
import com.dlc.shop.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author Yami
 */
@Slf4j
@Component("skuDeleteListener")
@AllArgsConstructor
public class SkuDeleteListener {

    private final PurchaseProdService purchaseProdService;
    private final SupplierProdService supplierProdService;
    private final TakeStockProdService takeStockProdService;
    private final StockBillLogItemService stockBillLogItemService;
    private final StockBillLogService stockBillLogService;
    private final SkuComboService skuComboService;
    private final StockPointSkuService stockPointSkuService;
    private final StockManager stockManager;


    @EventListener(SkuDeleteEvent.class)
    @Transactional(rollbackFor = Exception.class)
    public void prodChangeEvent(SkuDeleteEvent event) {
        if (CollUtil.isEmpty(event.getSkuIds())) {
            return;
        }
        List<Long> skuIds = event.getSkuIds();
        // 移除采购订单关联sku信息
        purchaseProdService.deleteByProdId(null, skuIds);
        List<Long> supplierIds = supplierProdService.listSupplierIdBySkuIds(skuIds);
        List<Long> takeStockIds = takeStockProdService.listTakeStockIdBySkuIds(skuIds);
        // 删除供应商商品关联sku信息
        supplierProdService.deleteByProdId(null, skuIds);
        // 删除正在盘点的商品的信息
        takeStockProdService.deleteByProdId(null, skuIds);
        // 删除sku出入库信息
        stockBillLogItemService.remove(new LambdaQueryWrapper<StockBillLogItem>().in(StockBillLogItem::getSkuId, skuIds));
        // 清除已经没有商品记录的出入库记录
        stockBillLogService.removeEmptyLog();
        // 如果是子商品的skuId在前面校验那一步就会删除,所以这里只需要删除组合商品主skuId关联的
        skuComboService.remove(new LambdaQueryWrapper<SkuCombo>().in(SkuCombo::getSkuId,skuIds));
        // 删除关联数据和库存
        List<StockPointSkuVO> pointSkuVOS = stockPointSkuService.listBySkuIds(skuIds);
        List<Long> stockPointIds = pointSkuVOS.stream().map(StockPointSkuVO::getStockPointSkuId).collect(Collectors.toList());
        List<SkuStockVO> skuStockVOList = BeanUtil.mapAsList(pointSkuVOS, SkuStockVO.class);
        skuStockVOList.forEach(s-> s.setStock(0));
        stockPointSkuService.deleteByIds(stockPointIds);
        stockManager.skuStock(skuStockVOList, LuaOperateEnum.SKU_REPLACE);
        //清除缓存
        for (Long supplierId : supplierIds) {
            supplierProdService.removeCacheBySupplierId(supplierId);
        }
    }
}
